﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using SamuelBlanchard.Extenders;
using Windows.System;

namespace Techdays2013
{
    public partial class AppToAppPage : PhoneApplicationPage
    {
        public AppToAppPage()
        {
            InitializeComponent();

            this.LayoutRoot.DataContext = this;

            this.MsSettings = new List<ListPickerItem>()
            {
                new ListPickerItem(){ Content="Mode avion", Tag="ms-settings-airplanemode:"},
                new ListPickerItem(){ Content="Bluetooth", Tag="ms-settings-bluetooth:"},
                new ListPickerItem(){ Content="Cellulaire", Tag="ms-settings-cellular:"},
                new ListPickerItem(){ Content="Wifi", Tag="ms-settings-wifi:"},
                new ListPickerItem(){ Content="Lock screen", Tag="ms-settings-lock:"},
                //new ListPickerItem(){ Content="Position", Tag="ms-settings-location:"}, 
                //new ListPickerItem(){ Content="Compte", Tag="ms-settings-accounts:"},
                //new ListPickerItem(){ Content="Email et compte", Tag="ms-settings-emailandaccounts:"}, 
            };

            this.CustomProtocols = new List<ListPickerItem>()
            {
                new ListPickerItem(){ Content="Une app", Tag="mon-app:"},
                new ListPickerItem(){ Content="Techdays2013", Tag="techdays2013://?said=Bonjour, je viens de la planète Protocole"},
                new ListPickerItem(){ Content="Runner", Tag="runner:"},
            };

            this.Loaded += TextToSpeechPage_Loaded;
            this.BackKeyPress += TextToSpeechPage_BackKeyPress;
            this.GridFade.Visibility = System.Windows.Visibility.Visible;        
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        void TextToSpeechPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.GridFade.Visibility = System.Windows.Visibility.Visible;
            this.GridFade.FadeIn((s) => { NavigationService.GoBack(); });
        }

        /// <summary>
        /// On attent sue tout soit mise à jour
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        void TextToSpeechPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.GridFade.FadeOut((s) => { s.Visibility = Visibility.Collapsed; });
        }

        /// <summary>
        /// Liste des apps système
        /// </summary>

        public List<ListPickerItem> MsSettings
        {
            get { return (List<ListPickerItem>)GetValue(MsSettingsProperty); }
            set { SetValue(MsSettingsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MsSettings.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MsSettingsProperty =
            DependencyProperty.Register("MsSettings", typeof(List<ListPickerItem>), typeof(AppToAppPage), new PropertyMetadata(null));

        /// <summary>
        /// Protocols Maison
        /// </summary>

        public List<ListPickerItem> CustomProtocols
        {
            get { return (List<ListPickerItem>)GetValue(CustomProtocolsProperty); }
            set { SetValue(CustomProtocolsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CustomProtocols.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CustomProtocolsProperty =
            DependencyProperty.Register("CustomProtocols", typeof(List<ListPickerItem>), typeof(AppToAppPage), new PropertyMetadata(null));

        

        /// <summary>
        /// Lancement de l'app systeme
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private async void Button_Click_AppSystem(object sender, RoutedEventArgs e)
        {
            var item = this.ListPickerAppSystem.SelectedItem as ListPickerItem;

            await Launcher.LaunchUriAsync(new Uri( (string)item.Tag, UriKind.Absolute) );
        }

        /// <summary>
        /// Appel du custom protocole
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private async void Button_Click_CustomProtocol(object sender, RoutedEventArgs e)
        {
            // C'et pareil que plus haut !

            var item = this.ListPickerCustomProtocols.SelectedItem as ListPickerItem;
            await Launcher.LaunchUriAsync(new Uri((string)item.Tag, UriKind.Absolute));
        }
    }
}